package org.slf4jx.core;

import org.slf4j.MDC;
import org.springframework.web.filter.OncePerRequestFilter;
import javax.servlet.ServletException;
import java.io.IOException;
/**
 * 用与web项目里吧head配置转成mdc配置， 并在访问结束后释放
 * @author jjliu
 * @date 2023/1/15
 */
public class Slf4jxFilter extends OncePerRequestFilter {
    @Override
    protected void doFilterInternal(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response, javax.servlet.FilterChain filterChain) throws ServletException, IOException {
        String slf4jxLevel = request.getHeader(Slf4jxCons.SLF4JX_HEAD_LEVEL);
        if(Slf4jxUtils.isNotBlank(slf4jxLevel)){
            MDC.put(Slf4jxCons.SLF4JX_MDC_LEVEL,slf4jxLevel);
        }
        try {
            filterChain.doFilter(request,response);
        } finally {
            MDC.remove(Slf4jxCons.SLF4JX_MDC_LEVEL);
        }
    }
}
